﻿using System;
using System.Collections.Generic;

namespace KKCai.HNKL.Test
{
    public class Utils
    {
        public const string FORMAT_N1 = "{0:D2}";
        public const string FORMAT_N2 = "{0:D2} {1:D2}";
        public const string FORMAT_N3 = "{0:D2} {1:D2} {2:D2}";
        public const string FORMAT_N4 = "{0:D2} {1:D2} {2:D2} {3:D2}";
        public const string FORMAT_N5 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2}";
        public const string FORMAT_N6 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2}";
        public const string FORMAT_N7 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2}";
        public const string FORMAT_N8 = "{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2}";


        /// <summary>
        /// 连二直(7种组合)
        /// </summary>
        public static List<string> GetA2(string n8)
        {
            List<string> list = new List<string>(7);

            string[] items = n8.Split(' ');

            list.Add(String.Format(FORMAT_N2, items[0], items[1]));
            list.Add(String.Format(FORMAT_N2, items[1], items[2]));
            list.Add(String.Format(FORMAT_N2, items[2], items[3]));
            list.Add(String.Format(FORMAT_N2, items[3], items[4]));
            list.Add(String.Format(FORMAT_N2, items[4], items[5]));
            list.Add(String.Format(FORMAT_N2, items[5], items[6]));
            list.Add(String.Format(FORMAT_N2, items[6], items[7]));

            return list;
        }

        /// <summary>
        /// 连二组(14种组合)
        /// </summary>
        public static List<string> GetP2(string n8)
        {
            List<string> list = new List<string>(14);

            string[] items = n8.Split(' ');

            list.Add(String.Format(FORMAT_N2, items[0], items[1]));
            list.Add(String.Format(FORMAT_N2, items[1], items[2]));
            list.Add(String.Format(FORMAT_N2, items[2], items[3]));
            list.Add(String.Format(FORMAT_N2, items[3], items[4]));
            list.Add(String.Format(FORMAT_N2, items[4], items[5]));
            list.Add(String.Format(FORMAT_N2, items[5], items[6]));
            list.Add(String.Format(FORMAT_N2, items[6], items[7]));

            list.Add(String.Format(FORMAT_N2, items[1], items[0]));
            list.Add(String.Format(FORMAT_N2, items[2], items[1]));
            list.Add(String.Format(FORMAT_N2, items[3], items[2]));
            list.Add(String.Format(FORMAT_N2, items[4], items[3]));
            list.Add(String.Format(FORMAT_N2, items[5], items[4]));
            list.Add(String.Format(FORMAT_N2, items[6], items[5]));
            list.Add(String.Format(FORMAT_N2, items[7], items[6]));

            return list;
        }


        #region 8个开奖号码组合,用于获取各类玩法中奖集合
        /// <summary>
        /// 8选2(28种组合)
        /// </summary>
        public static List<string> Get8P2(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(28);

            for (int i = 0; i <= 6; i++)
            {
                for (int j = i + 1; j <= 7; j++)
                {
                    list.Add(String.Format(FORMAT_N2, items[i], items[j]));
                }
            }

            return list;
        }

        /// <summary>
        /// 8选3(56种组合)
        /// </summary>
        public static List<string> Get8P3(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 5; i++)
            {
                for (int j = i + 1; j <= 6; j++)
                {
                    for (int k = j + 1; k <= 7; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, items[i], items[j], items[k]));
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选4(70种组合)
        /// </summary>
        public static List<string> Get8P4(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(70);

            for (int i = 0; i <= 4; i++)
            {
                for (int j = i + 1; j <= 5; j++)
                {
                    for (int k = j + 1; k <= 6; k++)
                    {
                        for (int m = k + 1; m <= 7; m++)
                        {
                            list.Add(String.Format(FORMAT_N4, items[i], items[j], items[k], items[m]));
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 8选5(56种组合)
        /// </summary>
        public static List<string> Get8P5(string n8)
        {
            string[] items = n8.Split(' ');

            List<string> list = new List<string>(56);

            for (int i = 0; i <= 3; i++)
            {
                for (int j = i + 1; j <= 4; j++)
                {
                    for (int k = j + 1; k <= 5; k++)
                    {
                        for (int m = k + 1; m <= 6; m++)
                        {
                            for (int n = m + 1; n <= 7; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, items[i], items[j], items[k], items[m], items[n]));
                            }
                        }
                    }
                }
            }

            return list;
        }

        #endregion

        #region 20个号码组合,用于初始化数据库

        /// <summary>
        /// 20选1(20种组合)
        /// </summary>
        public static List<string> Get20P1()
        {
            List<string> list = new List<string>(20);

            for (int i = 1; i <= 20; i++)
            {
                list.Add(String.Format(FORMAT_N1, i));
            }

            return list;
        }

        /// <summary>
        /// 20选2(190种组合)
        /// </summary>
        public static List<string> Get20P2()
        {
            List<string> list = new List<string>(190);

            for (int i = 1; i <= 19; i++)
            {
                for (int j = i + 1; j <= 20; j++)
                {
                    list.Add(String.Format(FORMAT_N2, i, j));
                }
            }

            return list;
        }

        /// <summary>
        /// 20选3(1140种组合)
        /// </summary>
        public static List<string> Get20P3()
        {
            List<string> list = new List<string>(1140);

            for (int i = 1; i <= 18; i++)
            {
                for (int j = i + 1; j <= 19; j++)
                {
                    for (int k = j + 1; k <= 20; k++)
                    {
                        list.Add(String.Format(FORMAT_N3, i, j, k));
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20选4(4845种组合)
        /// </summary>
        public static List<string> Get20P4()
        {
            List<string> list = new List<string>(4845);

            for (int i = 1; i <= 17; i++)
            {
                for (int j = i + 1; j <= 18; j++)
                {
                    for (int k = j + 1; k <= 19; k++)
                    {
                        for (int m = k + 1; m <= 20; m++)
                        {
                            list.Add(String.Format(FORMAT_N4, i, j, k, m));
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20选5(15504种组合)
        /// </summary>
        /// <returns></returns>
        public static List<string> Get20P5()
        {
            List<string> list = new List<string>(15504);

            for (int i = 1; i <= 16; i++)
            {
                for (int j = i + 1; j <= 17; j++)
                {
                    for (int k = j + 1; k <= 18; k++)
                    {
                        for (int m = k + 1; m <= 19; m++)
                        {
                            for (int n = m + 1; n <= 20; n++)
                            {
                                list.Add(String.Format(FORMAT_N5, i, j, k, m, n));
                            }
                        }
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// 20排列2(380种组合)
        /// </summary>
        public static List<string> Get20A2()
        {
            List<string> list = new List<string>(380);

            for (int i = 1; i <= 19; i++)
            {
                for (int j = i + 1; j <= 20; j++)
                {
                    list.Add(String.Format(FORMAT_N2, i, j));
                    list.Add(String.Format(FORMAT_N2, j, i));
                }
            }

            list.Sort();

            return list;
        }

        /// <summary>
        /// 20排列3(6840种组合)
        /// </summary>
        public static List<string> Get20A3()
        {
            List<string> list = new List<string>(6840);

            for (int i = 1; i <= 20; i++)
            {
                for (int j = 1; j <= 20; j++)
                {
                    if (i == j) continue;

                    for (int k = 1; k <= 20; k++)
                    {
                        if (i == k || j == k) continue;

                        list.Add(String.Format(FORMAT_N3, i, j, k));
                    }
                }
            }

            return list;
        }

        #endregion


        public static string Sort(string result)
        {
            object[] array = Array.ConvertAll(result.Split(' '), value => (object)value);

            Array.Sort<object>(array);

            switch (result.Split(' ').Length)
            {
                case 1: return String.Format(FORMAT_N1, array);
                case 2: return String.Format(FORMAT_N2, array);
                case 3: return String.Format(FORMAT_N3, array);
                case 4: return String.Format(FORMAT_N4, array);
                case 5: return String.Format(FORMAT_N5, array);
                case 6: return String.Format(FORMAT_N6, array);
                case 7: return String.Format(FORMAT_N7, array);
                case 8: return String.Format(FORMAT_N8, array);
                default: return result;
            }
        }

        public static int[] ConvertStringToIntArray(string value)
        {
            return Array.ConvertAll(value.Split(' '), val => Convert.ToInt32(val));
        }
    }
}
